﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using System.Configuration;
using System.Data;
using Newtonsoft.Json;

namespace PromotionEngine
{
    public partial class DocPropertyDetail : System.Web.UI.Page
    {
        public class ResponseDetail
        {
            public List<Model.DocPropertyDetail> Data { get; set; }
            public int Count { get; set; }
            public string Errors { get; set; }

            public ResponseDetail(List<Model.DocPropertyDetail> data, int count)
            {
                this.Data = data;
                this.Count = count;
            }

            public ResponseDetail(string errors)
            {
                this.Errors = errors;
            }

            public ResponseDetail()
            {

            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            string propertyID = Request.QueryString["PropertyID"];
            string propertyCode = Request.QueryString["PropertyCode"];
            string propertyName = Request.QueryString["PropertyName"];

            this.lblPropertyCode.Text = propertyCode;
            this.lblPropertyName.Text = propertyName;
            this.lblPropertyID.Value = propertyID;
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static ResponseDetail GetDocPropertyDetails(string propertyID)
        {
            List<Model.DocPropertyDetail> lstDocPropertyDetail = new List<Model.DocPropertyDetail>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetAllDocPropertyDetailByPropertyID(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), propertyID);
            if (dsPromotion != null)
            {
                foreach (DataRow r in dsPromotion.Tables[0].Rows)
                {
                    Model.DocPropertyDetail docPropertyDetail = new Model.DocPropertyDetail();
                    docPropertyDetail.PropertyDetailCode = r["PropertyDetailCode"].ToString();
                    docPropertyDetail.PropertyDetailName = r["PropertyDetailName"].ToString();
                    docPropertyDetail.PropertyDetailID = r["PropertyDetailID"].ToString();
                    docPropertyDetail.PropertyID = r["PropertyID"].ToString();
                    docPropertyDetail.CreatedUserID = r["CreatedUserID"].ToString();

                    lstDocPropertyDetail.Add(docPropertyDetail);
                }
                var jsonData = new { total = dsPromotion.Tables[0].Rows.Count, lstCustomerProperty = lstDocPropertyDetail };
                return new ResponseDetail(lstDocPropertyDetail, dsPromotion.Tables[0].Rows.Count);
            }
            return new ResponseDetail();
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static void UpdateDocPropertyDetail(string oDocPropertyDetail, bool isInsert)
        {
            Model.DocPropertyDetail customerPropertyDetail = JsonConvert.DeserializeObject<Model.DocPropertyDetail>(oDocPropertyDetail);
            if (isInsert)
            {
                if (HttpContext.Current.Session["LoginUser"] != null)
                {
                    DataTable tbUser = (DataTable)HttpContext.Current.Session["LoginUser"];

                    if (tbUser != null && tbUser.Rows.Count > 0)
                    {
                        customerPropertyDetail.CreatedUserID = tbUser.Rows[0]["UserID"].ToString();
                    }
                }
            }
            Promotion pmt = new Promotion();
            string errMsg = pmt.UpdateJsonDocPropertyDetail(customerPropertyDetail.PropertyID, customerPropertyDetail.PropertyDetailCode, customerPropertyDetail.PropertyDetailName
                                                    , customerPropertyDetail.PropertyDetailID, customerPropertyDetail.CreatedUserID,
                  ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), isInsert);
            if (errMsg.Length == 0)
            {
                //Show error message
            }
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static void DeleteDocPropertyDetail(string propertyDetailID)
        {
            Promotion pmt = new Promotion();
            string errMsg = pmt.DeleteJsonDocPropertyDetail(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), propertyDetailID);
            if (errMsg.Length == 0)
            {
                //Show error message
            }
        }
    }
}